package com.work.chat.common.util;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * JSON对象工具类
 */
public class JsonUtil {
    private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * json字符串转对象
     *
     * @param json
     * @param object
     * @param <T>
     * @return
     */
    public static <T> T strToObject(Object json, Class<T> object) {
        try {
            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            //忽略在json字符串中存在,但是在java对象中不存在对应属性的情况
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper.readValue(objectToStr(json), object);
        } catch (JsonProcessingException e) {
            logger.error(e.toString());
            throw new RuntimeException(object + "转换失败");
        }
    }


    /**
     * 对象转json字符串
     *
     * @param object
     * @return
     */
    public static String objectToStr(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error(e.toString());
            return null;
        }
    }

    /**
     * json字符串数组转实体对象
     *
     * @param json
     * @param object
     * @param <T>
     */
    public static <T> List<T> strToList(Object json, Class<T> object) {
        if (!ObjectUtils.isEmpty(json)) {
            try {
                objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
                //忽略在json字符串中存在,但是在java对象中不存在对应属性的情况
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, object);
                return objectMapper.readValue(objectToStr(json), listType);
            } catch (JsonProcessingException e) {
                logger.error(e.toString());
            }
        }
        return new ArrayList<>();

    }

    /**
     * 获取JsonNode对象
     * @param obj
     * @return
     */
    public static JsonNode getJsonNode(String obj){
        try {
            return objectMapper.readTree(obj);
        } catch (JsonProcessingException e) {
            logger.error(e.toString());
            return null;
        }
    }

    /**
     * Json判断是否为空
     *
     * @param jsonNode
     * @return 返回字符串
     */
    public static String isNotNullJson(JsonNode jsonNode) {
        if (ObjectUtils.isEmpty(jsonNode)) {
            return null;
        }
        return jsonNode.asText();
    }

    /**
     * json字符串转数组字符串
     *
     * @param arrayJson
     * @return 返回数组字符串
     */
    public static String jsonStr(String arrayJson) {
        if (ObjectUtils.isEmpty(arrayJson)) return null;
        return arrayJson.replace("[", "").replace("]", "").replaceAll("\"", "");
    }
}
